package com.github.elliot.usercenter.dao.impl;

import com.github.elliot.usercenter.config.R2dbcPageAgent;
import com.github.elliot.usercenter.dao.UserInnerRepository;
import com.github.elliot.usercenter.po.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
import org.springframework.data.relational.core.query.Criteria;
import org.springframework.data.relational.core.query.Query;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

import java.util.List;

@Service
public class UserInnerRepositoryImpl implements UserInnerRepository {

    @Autowired
    private R2dbcEntityTemplate r2dbcEntityTemplate;

    @Override
    public Mono<Page<User>> pageList(List<Criteria> criteriaList, int current, int size) {
        Query query = Query.query(Criteria.from(criteriaList)).columns("id, user_no, username")
                .sort(Sort.by(Order.desc("create_time")).and(Sort.by(Order.desc("id"))));
        R2dbcPageAgent<User> page = new R2dbcPageAgent<>(r2dbcEntityTemplate, User.class);
        return page.findPageByQuery(query, current, size);
    }
}
